package com.msmall.community.wiki.action.html;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.klg.model.Article;
import com.msmall.community.wiki.model.WikiSegmentArticle;
import com.msmall.community.wiki.service.WikiManager;

public class WikiAction extends HTMLBaseAction{

	private static final long serialVersionUID = 4173016537893993077L;

	private Long segid;
	
	private Long articleid;
	
	private Long parentType;
	private Long middleType;
	private Long valueType;
	
	private Article article;
	
	private CascadeCollection<Segment> motherWiki;
	
	private CascadeCollection<Segment> babyWiki;
	
	private CascadeCollection<Segment> wikiSegment;
	
	private WikiManager wikiManager;
	
	public void setWikiManager(WikiManager wikiManager) {
		this.wikiManager = wikiManager;
	}

	public String execute(){
		try{
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			CascadeCollection<Segment> wikiSegments = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_WIKI_NAV);
			motherWiki = wikiSegments.getChildren().get(0);
			babyWiki = wikiSegments.getChildren().get(1);
		}catch(Exception e){
			log.error(e);
		}
		return SUCCESS;
	}
	
	public String view(){
		try {
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			wikiSegment = cs.getAllDatas().get(segid);
			article = wikiManager.getBySegmentid(segid);
			wikiManager.getPaginationByArticle(page, getUserid(), article.getArticleid());
			page.setUrl(ConstantUrl.HTML_KLG_ARTICLE_REPLY_PAGINATION + "?articleid="+ article.getArticleid());
		} catch (Exception e) {
			log.error(e);
		}
		return SUCCESS;
	}

	public String create(){
		return SUCCESS;
	}
	public String update(){
		try {
			article = (Article) wikiManager.get(Article.class, articleid);
			
			WikiSegmentArticle wsa = wikiManager.getByArticleid(articleid);
			if(wsa != null){
				CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
				CascadeCollection<Segment> current = cs.getAllDatas().get(wsa.getSegmentid());
				CascadeCollection<Segment> middle = current.getParents().get(0);
				CascadeCollection<Segment> parent = middle.getParents().get(0);
				valueType = wsa.getSegmentid();
				middleType = middle.getCurrent().getSegmentid();
				parentType = parent.getCurrent().getSegmentid();
			}
		} catch (Exception e) {
			log.error(e);
		}
		return SUCCESS;
	}
	public String save(){
		try{
			article = wikiManager.saveWiki(article, valueType, getUserid(), getUserAgentType());
			segid = valueType;
		}catch(Exception e){
			log.error(e);
		}
		return SUCCESS;
	}
	
	public Long getSegid() {
		return segid;
	}

	public void setSegid(Long segid) {
		this.segid = segid;
	}

	public CascadeCollection<Segment> getMotherWiki() {
		return motherWiki;
	}

	public void setMotherWiki(CascadeCollection<Segment> motherWiki) {
		this.motherWiki = motherWiki;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public CascadeCollection<Segment> getBabyWiki() {
		return babyWiki;
	}

	public void setBabyWiki(CascadeCollection<Segment> babyWiki) {
		this.babyWiki = babyWiki;
	}

	public Long getValueType() {
		return valueType;
	}

	public void setValueType(Long valueType) {
		this.valueType = valueType;
	}

	public Long getArticleid() {
		return articleid;
	}

	public void setArticleid(Long articleid) {
		this.articleid = articleid;
	}

	public Long getParentType() {
		return parentType;
	}

	public void setParentType(Long parentType) {
		this.parentType = parentType;
	}

	public Long getMiddleType() {
		return middleType;
	}

	public void setMiddleType(Long middleType) {
		this.middleType = middleType;
	}

	public CascadeCollection<Segment> getWikiSegment() {
		return wikiSegment;
	}

	public void setWikiSegment(CascadeCollection<Segment> wikiSegment) {
		this.wikiSegment = wikiSegment;
	}
}
